<script setup lang="ts">
import { getHotInVogue, getHotNew, getHotOneStop, getHotPreference } from '@/api/hot'

import type { HotItem, HotParams, HotResult } from '@/types/hot'
import { onMounted, ref } from 'vue'

const props = defineProps<{
  type: string
}>()
const hotParams = ref<HotParams>({
  subType: undefined,
  page: 1,
  pageSize: 10,
})
const hotList = ref<HotResult>()
const goodsData = ref<HotItem[]>([])
const goodsData2 = ref<HotItem[]>([])
const pageAll = ref<number>()
const getHotList = async () => {
  hotList.value = {
    bannerPicture: '',
    id: '',
    subTypes: [],
    title: '',
  }
  if (props.type === '1') {
    const res = await getHotPreference(hotParams.value)
    hotList.value = res.result
  } else if (props.type === '2') {
    const res = await getHotInVogue(hotParams.value)
    hotList.value = res.result
  } else if (props.type === '3') {
    const res = await getHotOneStop(hotParams.value)
    hotList.value = res.result
  } else {
    const res = await getHotNew(hotParams.value)
    hotList.value = res.result
  }
  if (hotList.value?.subTypes[0]?.goodsItems.items) {
    goodsData.value.push(...hotList.value.subTypes[0].goodsItems.items)
  }
  if (hotList.value?.subTypes[1]?.goodsItems.items) {
    goodsData2.value.push(...hotList.value.subTypes[1].goodsItems.items)
  }
  pageAll.value = hotList.value?.subTypes[0]?.goodsItems.pages
}

const flag = ref(1)
onMounted(() => {
  getHotList()
})
// 下滑加载
const isOver = ref(false)
const getMore = async () => {
  if (isOver.value) {
    return uni.showToast({ title: '没有更多数据', icon: 'none' })
  }
  hotParams.value.page = hotParams.value.page + 1
  await getHotList()
  if (hotParams.value.page >= pageAll.value!) {
    isOver.value = true
  }
}
// 下拉刷新
const refresherTriggered = ref(false)
const onRefresh = async () => {
  refresherTriggered.value = true
  hotParams.value.page = 1
  hotParams.value.page++
  goodsData.value = []
  goodsData2.value = []
  await getHotList()
  refresherTriggered.value = false
}
</script>

<template>
  <view class="viewport">
    <!-- 推荐封面图 -->
    <view class="cover">
      <image :src="hotList?.bannerPicture" mode="widthFix"></image>
    </view>
    <!-- 推荐选项 -->
    <view class="tabs">
      <text class="text" :class="{ active: flag === 1 }" @click="flag = 1">{{
        hotList?.subTypes[0]?.title
      }}</text>
      <text class="text" :class="{ active: flag === 2 }" @click="flag = 2">{{
        hotList?.subTypes[1]?.title
      }}</text>
    </view>
    <!-- 推荐列表 -->
    <scroll-view
      scroll-y
      class="scroll-view"
      @scrolltolower="getMore"
      :refresher-enabled="true"
      :refresher-triggered="refresherTriggered"
      @refresherrefresh="onRefresh"
    >
      <view class="goods" v-if="flag === 1">
        <navigator
          hover-class="none"
          class="navigator"
          v-for="goods in goodsData"
          :key="goods.id"
          :url="`/pages/goods/goods?id=${goods.id}`"
        >
          <image class="thumb" :src="goods.picture"></image>
          <view class="name ellipsis">{{ goods.desc }}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{ goods.price }}</text>
          </view>
        </navigator>
      </view>
      <view class="goods" v-else>
        <navigator
          hover-class="none"
          class="navigator"
          v-for="goods in goodsData2"
          :key="goods.id"
          :url="`/pages/goods/goods`"
        >
          <image class="thumb" :src="goods.picture"></image>
          <view class="name ellipsis">{{ goods.desc }}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{ goods.price }}</text>
          </view>
        </navigator>
      </view>
      <view class="loading-text">
        {{ isOver ? '没有更多数据' : '正在加载...' }}
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: #f4f4f4;
}
.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}
.cover {
  width: 1000rpx;
  // height: 225rpx;
  // border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;
  position: absolute;
  left: 20rpx;
  top: 0;
}
.scroll-view {
  flex: 1;
  height: 100vh;
}
.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
  .text {
    margin: 0 20rpx;
    position: relative;
  }
  .active {
    &::after {
      content: '';
      width: 40rpx;
      height: 4rpx;
      transform: translate(-50%);
      background-color: #27ba9b;
      position: absolute;
      left: 50%;
      bottom: 24rpx;
    }
  }
}
.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
  .navigator {
    width: 345rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }
  .thumb {
    width: 305rpx;
    height: 305rpx;
  }
  .name {
    height: 88rpx;
    font-size: 26rpx;
  }
  .price {
    line-height: 1;
    color: #cf4444;
    font-size: 30rpx;
  }
  .symbol {
    font-size: 70%;
  }
  .decimal {
    font-size: 70%;
  }
}

.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>
